package com.example.filter;

import com.example.pojo.Payload;
import com.example.pojo.User;
import com.example.utils.JwtUtil;
import com.example.utils.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/3/15 20:08
 */
@Slf4j
@Component
public class GlobalFilter implements Ordered, org.springframework.cloud.gateway.filter.GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // ServerHttpRequest request = exchange.getRequest();
        // ServerHttpResponse response = exchange.getResponse();
        // // 校验token是否合法
        // Payload<User> payload = null;
        // try {
        //     String token = request.getCookies().getFirst("token").getValue();
        //     payload = JwtUtil.getInfoFromToken(token, RSAUtil.getPublicKey(RSAUtil.PUB_KEY_PATH), User.class);
        //     System.out.println("token合法");
        // } catch (Exception e) {
        //     e.printStackTrace();
        //     response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //     return response.setComplete();
        // }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
